"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : show variables
Case Name   : 修改参数值后进行查询 alter
Create At   : 2022/09/20
@zou_jialiang0501120110
Description :
    1、create database
    2、修改数据库参数值
    3、查询参数query_dop
    4、set设置会话级别参数query_dop并查询
    5、alter system修改参数值autovacuum
    6、查询参数autovacuum
    7、清理环境
Expect      :
    1、创建成功
    2、修改成功
    3、查询成功
    4、设置成功
    5、修改成功
    6、查询成功
    7、清理环境成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class VariablesCase(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.constant = Constant()
        self.db_name = "db_var_0018"
        self.q_guc = "query_dop"
        self.v_guc = "autovacuum"
        self.initial_query_dop = self.common.show_param(f"{self.q_guc}")
        self.initial_autovacuum = self.common.show_param(f"{self.v_guc}")

    def test_sensitive(self):
        step1_text = "-----step1:create database;expect:创建成功-----"
        self.logger.info(step1_text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
                    create database {self.db_name} dbcompatibility = 'B';'''
        self.logger.info(sql_cmd1)
        sql_res1 = self.commonsh.execut_db_sql(sql_cmd1)
        self.logger.info(sql_res1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res1,
                      "create database filed:" + step1_text)

        step2_text = "---step2:修改数据库参数值;expect:修改成功---"
        self.logger.info(step2_text)
        sql_cmd2 = f"alter database {self.db_name} set {self.q_guc} to 2;"
        self.logger.info(sql_cmd2)
        sql_res2 = self.commonsh.execut_db_sql(sql_cmd2)
        self.logger.info(sql_res2)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG,
                      sql_res2, "reset GUC filed:" + step2_text)

        step3_text = "-----step3:查询参数query_dop;expect:查询成功-----"
        self.logger.info(step3_text)
        sql_cmd3 = f"show variables where variable_name like '{self.q_guc}';"
        self.logger.info(sql_cmd3)
        sql_res3 = self.commonsh.execut_db_sql(sql_cmd3,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res3)
        self.common.equal_sql_mdg(
            sql_res3, "Variable_name | Value ", f" {self.q_guc}     | 2",
            "(1 row)", flag="1")
        sql_cmd4 = f"show session variables " \
                   f"where variable_name like '{self.q_guc}';"
        self.logger.info(sql_cmd4)
        sql_res4 = self.commonsh.execut_db_sql(sql_cmd4,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res4)
        self.common.equal_sql_mdg(
            sql_res4, "Variable_name | Value ", f" {self.q_guc}     | 2",
            "(1 row)", flag="1")
        sql_cmd5 = f"show global variables " \
                   f"where variable_name like '{self.q_guc}';"
        self.logger.info(sql_cmd5)
        sql_res5 = self.commonsh.execut_db_sql(sql_cmd5,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res5)
        self.common.equal_sql_mdg(
            sql_res5, "Variable_name | Value ", f" {self.q_guc}     | 2",
            "(1 row)", flag="1")

        step4_text = "-----step4:set设置会话级别参数query_dop并查询;" \
                     "expect:查询成功-----"
        self.logger.info(step4_text)
        sql_cmd6 = f"set {self.q_guc} to 4;" \
                   f"show variables where variable_name like '{self.q_guc}';"
        self.logger.info(sql_cmd6)
        sql_res6 = self.commonsh.execut_db_sql(sql_cmd6,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res6)
        self.common.equal_sql_mdg(sql_res6, "SET", "Variable_name | Value ",
                                  f" {self.q_guc}     | 4",
                                  "(1 row)", flag="2")
        sql_cmd7 = f"set {self.q_guc} to 4;" \
                   f"show session variables " \
                   f"where variable_name like '{self.q_guc}';"
        self.logger.info(sql_cmd7)
        sql_res7 = self.commonsh.execut_db_sql(sql_cmd7,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res7)
        self.common.equal_sql_mdg(sql_res7, "SET", "Variable_name | Value ",
                                  f" {self.q_guc}     | 4",
                                  "(1 row)", flag="2")
        sql_cmd8 = f"set {self.q_guc} to 4;" \
                   f"show global variables " \
                   f"where variable_name like '{self.q_guc}';"
        self.logger.info(sql_cmd8)
        sql_res8 = self.commonsh.execut_db_sql(sql_cmd8,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res8)
        self.common.equal_sql_mdg(sql_res8, "SET", "Variable_name | Value ",
                                  f" {self.q_guc}     | 2",
                                  "(1 row)", flag="2")

        step5_text = "-----step5:alter system修改参数值autovacuum;" \
                     "expect:修改成功-----"
        self.logger.info(step5_text)
        sql_cmd9 = f"alter system set {self.v_guc} to off;"
        self.logger.info(sql_cmd9)
        sql_res9 = self.commonsh.execut_db_sql(sql_cmd9,
                                               dbname=f"{self.db_name}")
        self.logger.info(sql_res9)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, sql_res9,
                      "alter system filed:" + step5_text)

        step6_text = "-----step6:查询参数autovacuum;expect:查询成功-----"
        self.logger.info(step6_text)
        sql_cmd10 = f"show variables where variable_name like '{self.v_guc}';"
        self.logger.info(sql_cmd10)
        sql_res10 = self.commonsh.execut_db_sql(sql_cmd10,
                                                dbname=f"{self.db_name}")
        self.logger.info(sql_res10)
        self.common.equal_sql_mdg(sql_res10, "Variable_name | Value ",
                                  f" {self.v_guc}    | off",
                                  "(1 row)", flag="1")
        sql_cmd11 = f"show session variables " \
                    f"where variable_name like '{self.v_guc}';"
        self.logger.info(sql_cmd11)
        sql_res11 = self.commonsh.execut_db_sql(sql_cmd11,
                                                dbname=f"{self.db_name}")
        self.logger.info(sql_res11)
        self.common.equal_sql_mdg(sql_res11, "Variable_name | Value ",
                                  f" {self.v_guc}    | off",
                                  "(1 row)", flag="1")
        sql_cmd12 = f"show global variables " \
                    f"where variable_name like '{self.v_guc}';"
        self.logger.info(sql_cmd12)
        sql_res12 = self.commonsh.execut_db_sql(sql_cmd12,
                                                dbname=f"{self.db_name}")
        self.logger.info(sql_res12)
        self.common.equal_sql_mdg(sql_res12, "Variable_name | Value ",
                                  f" {self.v_guc}    | off",
                                  "(1 row)", flag="1")

    def tearDown(self):
        step7_text = "-----step7:清理环境;expect:成功-----"
        self.logger.info(step7_text)

        drop_db_text = "-----drop database-----"
        self.logger.info(drop_db_text)
        drop_db_cmd = f'''drop database if exists {self.db_name};'''
        self.logger.info(drop_db_cmd)
        drop_db_res = self.commonsh.execut_db_sql(drop_db_cmd)
        self.logger.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      drop_db_res, "drop database filed" + drop_db_res)

        reset_text = "-----reset GUC-----"
        self.logger.info(reset_text)
        re_cmd1 = self.commonsh.execute_gsguc("set",
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f"{self.q_guc} = "
                                              f"{self.initial_query_dop}")
        self.logger.info(re_cmd1)

        re_cmd2 = self.commonsh.execute_gsguc("set",
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f"{self.v_guc} = "
                                              f"{self.initial_autovacuum}")
        self.logger.info(re_cmd2)

        restart_text = "-----restart database-----"
        self.logger.info(restart_text)
        restart_cmd = self.commonsh.restart_db_cluster()
        self.logger.info(restart_cmd)
        self.logger.info("-----check database status-----")
        status_res = self.commonsh.get_db_cluster_status()

        self.assertTrue(re_cmd1,
                        f"reset GUC {self.initial_query_dop} filed:"
                        + reset_text)
        self.assertTrue(re_cmd2,
                        f"reset GUC {self.initial_autovacuum} filed::"
                        + reset_text)
        self.assertTrue("Degraded" in status_res or "Normal" in status_res)

        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
